# coding: utf-8

from wtforms import StringField, BooleanField
from wtforms.validators import Optional, DataRequired, Regexp
from wtforms_components import DateTimeField, read_only
from ..constant import Choice
from ..forms import (
    WphForm, NonValidatingSelectField, NotChoices, MyDateTimeField
)
from .constant import (
    CallResultDisplay, VirtualSmsMessageResult, VirtualNumberValid
)


class SearchCallOrderForm(WphForm):
    """
    搜索话单表单
    """
    outer_user = NonValidatingSelectField('外部工程商', coerce=int,
        choices=NotChoices + Choice.to_options(),
        validators=[Optional()], default=NotChoices[0][0]
    )
    caller = StringField('主叫真实号码', validators=[Optional()])
    called = StringField('被叫真实号码', validators=[Optional()])
    call_result = NonValidatingSelectField('通话状态',
        choices=NotChoices + list(CallResultDisplay.items()),
        validators=[Optional()], default=NotChoices[0][0]
    )


class SearchMessageForm(WphForm):
    """
    短信信息显示
    create by yuanxing at 2017/10/23
    """

    sms_result = NonValidatingSelectField('发送结果',
        choices=NotChoices + VirtualSmsMessageResult.to_options(),
        validators=[Optional()], default=NotChoices[0][0]
    )
    sender = StringField('发送者真实号码', validators=[Optional()])
    receiver = StringField('接收者真实号码', validators=[Optional()])
    transfer_time_begin = DateTimeField('最早发送时刻', validators=[Optional()],)
    transfer_time_end = DateTimeField('最晚发送时刻', validators=[Optional()],)
    sms_content = StringField('短信内容', validators=[Optional()])


class SearchCallNumberRecord(WphForm):
    """
    虚拟号呼叫记录查询
    create by yuanxing at 2017/12/15
    """
    created_at_begin = MyDateTimeField('最早创建时间', validators=[Optional()], )
    created_at_end = MyDateTimeField('最晚创建时间', validators=[Optional()], )
    caller = StringField('主叫真实号码', validators=[Optional()])
    called = StringField('绑定虚拟号', validators=[Optional()])
    lt_called = StringField('被叫真实号码', validators=[Optional()])


class SearchSmsNumberRecord(WphForm):
    """
    虚拟号短信记录查询
    add by zhouhl 2018-07-03
    """
    created_at_begin = MyDateTimeField('最早创建时间', validators=[Optional()], )
    created_at_end = MyDateTimeField('最晚创建时间', validators=[Optional()], )
    receiver = StringField('接收虚拟号', validators=[Optional()])
    sender = StringField('真实发送号', validators=[Optional()])


class SearchNumberRelationForm(WphForm):
    """
    create by yuanxing at 2017/10/13
    """
    tel_a = StringField('号码a', validators=[Optional()])
    tel_b = StringField('号码b', validators=[Optional()])
    tel_x = StringField('虚拟号', validators=[Optional()])
    demand_id = StringField('项目ID', validators=[Optional()])
    for_axb = BooleanField('是否为AxB', default=False, validators=[Optional()])
    valid = NonValidatingSelectField('是否有效', coerce=int,
        choices=NotChoices + VirtualNumberValid.to_options(),
        validators=[Optional()], default=NotChoices[0][0]
    )
    created_at_begin = MyDateTimeField('最早绑定时间', validators=[Optional()], )
    created_at_end = MyDateTimeField('最晚绑定时间', validators=[Optional()], )
    updated_at_begin = MyDateTimeField('最早解绑时间', validators=[Optional()], )
    updated_at_end = MyDateTimeField('最晚解绑时间', validators=[Optional()], )


class AddVirtualNumberForm(WphForm):
    """
    新增虚拟号关系表单
    """
    tel_a = StringField('号码a', validators=[DataRequired(), Regexp('^[0-9]*$')])
    tel_b = StringField('号码b', validators=[Optional(), Regexp('^[0-9]*$')])
    tel_x = StringField('虚拟号', validators=[DataRequired(), Regexp('^[0-9]*$')])


class DetailVirtualNumberForm(WphForm):
    """
    编辑虚拟号详情表单
    """
    tel_a = StringField('号码a', validators=[Optional()])
    tel_b = StringField('号码b', validators=[Optional()])
    tel_x = StringField('虚拟号', validators=[Optional()])
    valid = NonValidatingSelectField('是否有效', coerce=int,
        choices=VirtualNumberValid.to_options(),
        validators=[Optional()], default=NotChoices[0][0]
    )

    def __init__(self, *args, **kwargs):
        super(DetailVirtualNumberForm, self).__init__(*args, **kwargs)
        read_only(self.tel_a)
        read_only(self.tel_b)
        read_only(self.tel_x)
